$(function () {
      getUserInfo()
      // 退出登录
      $('#logout').on('click', function () {
            layer.confirm('确认退出吗?', { icon: 3, title: '提示' }, function (index) {
                  localStorage.removeItem('token')
                  location.href = '/login.html'
                  layer.close(index)
            })
      })
})
function getUserInfo() {
      axios({
            method: 'GET',
            url: '/my/userinfo',
            headers: {
                  Authorization: localStorage.getItem('token')
            }
      }).then(res => {
            // console.log(res);
            // console.log(res.data);
            renderAvatar(res.data.data)
      })
}
// 渲染用户信息和头像
function renderAvatar(user) {
      console.log(user);
      let name = user.nickname || user.username
      $('.welcom').html('欢迎&nbsp' + name)
      if (user.user_pic !== null) {
            $('.avatar-text').hide()
            $('.layui-nav-img').show().attr('src', user.user_pic)
      } else {
            $('.layui-nav-img').hide()
            let first = name[0].toUpperCase()
            $('.avatar-text').show().html(first)
      }
}
